package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author zzw
 * @date 2025/4/29
 * @description 菜品管理控制器
 */
@Api(tags = "菜品管理接口")
@Slf4j
@RequestMapping("/admin/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO 菜品分页查询参数
     * @return Result<PageResult < DishVO>>
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult<DishVO>> page(@ApiParam("菜品分页查询参数") DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult<DishVO> pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO 菜品信息
     * @return Result<String>
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> update(@ApiParam("菜品信息") @RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);

        // 缓存优化，清理缓存，将菜品缓存全部删除
        Set<String> keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id 菜品id
     * @return Result<DishVO>
     */
    @ApiOperation("根据ID查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@ApiParam(value = "菜品ID", required = true) @PathVariable Long id) {
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 启用禁用菜品
     *
     * @param status 状态
     * @param id     菜品id
     * @return Result<String>
     */
    @ApiOperation("启用/禁用菜品")
    @PostMapping("/status/{status}")
    public Result<String> status(@ApiParam(value = "状态(0禁用1启用)", required = true) @PathVariable Integer status,
                                 @ApiParam(value = "菜品ID", required = true) Long id) {
        log.info("启用禁用菜品：{}", status);
        dishService.status(status, id);

        // 缓存优化，清理缓存，将菜品缓存全部删除
        Set<String> keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**
     * 批量删除菜品
     *
     * @param ids 菜品id
     * @return Result<String>
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result<String> deleteBatch(@ApiParam(value = "菜品ID列表", required = true) @RequestParam List<Long> ids) {
        log.info("批量删除菜品：{}", ids);
        dishService.deleteBatch(ids);

        // 缓存优化，清理缓存，将菜品缓存全部删除
        Set<String> keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**
     * 新增菜品
     *
     * @param dishDTO 菜品DTO
     * @return Result<String>
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> save(@ApiParam("菜品信息") @RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        // 缓存优化，清理缓存
        redisTemplate.delete("dish_" + dishDTO.getCategoryId());

        return Result.success();
    }

    /**
     * 根据分类id或者菜品名称查询菜品
     *
     * @param categoryId 分类id
     * @param name       菜品名称
     * @return Result<List < Dish>>
     */
    @ApiOperation("根据分类ID或名称查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getByCategoryIdOrName(@ApiParam("分类ID") @RequestParam(required = false) Long categoryId,
                                                    @ApiParam("菜品名称") @RequestParam(required = false) String name) {
        log.info("根据分类id或者名称查询菜品：{},{}", categoryId, name);
        List<Dish> list = dishService.getByCategoryIdOrName(categoryId, name);
        return Result.success(list);
    }
}
